package com.example.wyr;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    TextView out;
    EditText inp1, inp2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//以R开头表示在res目录下
        out = (TextView) findViewById(R.id.showText);
        inp1 = (EditText) findViewById(R.id.inp1);
        inp2 = (EditText) findViewById(R.id.inp2);
        Button btn = (Button) findViewById(R.id.button);
    }

    public void btnClick(View btn) {
        showText();
    }

    public boolean isDouble(String s) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达
        Matcher match = pattern.matcher(s);
        boolean bo = match.matches();
        return bo;
    }

    private void showText() {
        Log.i("show", "weight=" + inp1.getText().toString());
        Log.i("show", "height=" + inp2.getText().toString());
        String t1 = (String) inp1.getText().toString();
        String t2 = (String) inp2.getText().toString();
        if (isDouble(t1) & isDouble(t2)) {
            double t3 = Double.parseDouble(t1) / (Double.parseDouble(t2) * Double.parseDouble(t2));
            if (t3 < 15) {
                out.setText("您的BMI为：" + String.format("%.2f", t3) + "属于饥饿");
            } else if (t3 < 17.4 & t3 > 15) {
                out.setText("您的BMI为：" + String.format("%.2f", t3) + "属于厌食症");
            } else if (t3 < 18.4 & t3 > 17.5) {
                out.setText("您的BMI为：" + String.format("%.2f", t3) + "属于过轻");
            } else if (t3 < 24.9 & t3 > 18.4) {
                out.setText("您的BMI为：" + String.format("%.2f", t3) + "属于理想");
            } else if (t3 < 29.9 & t3 > 25) {
                out.setText("您的BMI为：" + String.format("%.2f", t3) + "属于超重");
            } else {
                out.setText("您的BMI为：" + String.format("%.2f", t3) + "属于肥胖");
            }
        }
        else{
            out.setText("请输入正确的数字！");
        }
    }
}